package com.wskj.web.controller.common;

import com.wskj.common.core.domain.AjaxResult;
import com.wskj.obs.service.IObsService;
import com.wskj.system.domain.SysCommon;
import com.wskj.system.service.ISysCommonService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;

/**
 * OBS Controller
 *
 * @author Kun
 * @date 2021-11-23
 */
@Api(value = "OBS控制器", tags = {"OBS管理"})
@AllArgsConstructor
@RestController
@RequestMapping("/obs")
public class ObsController {

    private final ISysCommonService iSysCommonService;
    private final IObsService iObsService;

    @ApiOperation("文件上传")
    @PostMapping("/upload")
    public AjaxResult upload(@RequestParam(value = "file") MultipartFile file) {
        // 上传方式 1:华为云 2:腾讯云  3： 阿里云oss 4：本地
        SysCommon dbSysCommon = iSysCommonService.getById(1000043L);
        try {
            Map<String,Object> fileUrl = new HashMap<>();
            if (dbSysCommon == null || "1".equals(dbSysCommon.getCommonValue())) {
                // 默认华为云存储;
                fileUrl = iObsService.uploadByHuaWei(file);
            } else if ("2".equals(dbSysCommon.getCommonValue())) {
                // 腾讯云存储
                fileUrl = iObsService.uploadByTencent(file);
            } else if ("3".equals(dbSysCommon.getCommonValue())) {
                // 阿里云存储
                fileUrl = iObsService.uploadByOss(file);
            } else {
                // 本地存储
                fileUrl = iObsService.uploadByLocal(file);
            }
            return AjaxResult.success("上传成功",fileUrl);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("上传失败=>"+e.getMessage());
        }
    }
}
